package com.center.growcenterserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.center.growcenterserver.common.R;
import com.center.growcenterserver.mapper.StudentCourseMapper;
import com.center.growcenterserver.model.domain.Student;
import com.center.growcenterserver.model.domain.StudentCourse;
import com.center.growcenterserver.service.StudentCoursesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
@Service
public class StudentCoursesServiceImpl implements StudentCoursesService {

    @Autowired
    private StudentCourseMapper studentCourseMapper;

    @Override
    public R allStudentCourses(IPage<StudentCourse> page) {
        IPage<StudentCourse> selectPage = studentCourseMapper.selectPage(page, null);
        return R.success("查询成功", selectPage);
    }


    @Override
//    public R getStudentCoursesById(String studentId, IPage<StudentCourse> page) {
    public R getStudentCoursesById(String studentId, IPage<StudentCourse> page) {
        // 创建查询条件，指定 studentId
        LambdaQueryWrapper<StudentCourse> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StudentCourse::getStudentId, studentId);

        // 执行分页查询，这里会自动计算总记录数
        IPage<StudentCourse> resultPage = studentCourseMapper.selectPage(page, queryWrapper);
//        IPage<StudentCourse> resultPage = studentCourseMapper.findCoursesByStudentId(page, studentId);

        // 获取课程总数
        Integer totalCoursesCount = ((int) resultPage.getTotal());

        // 计算平均成绩
        BigDecimal averageGrade = resultPage.getRecords().stream()
                .map(StudentCourse::getGrade)
                .filter(grade -> grade != null)
                .reduce(BigDecimal.ZERO, BigDecimal::add)
                .divide(BigDecimal.valueOf(resultPage.getRecords().stream()
                        .filter(record -> record.getGrade() != null).count()), BigDecimal.ROUND_HALF_UP);


        // 检查结果是否为空，并构建响应
        if (resultPage.getRecords().isEmpty()) {
            return R.error("未找到该学生的课程成绩");
        } else {
            // 构建一个Map来保存分页数据和课程总数
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("page", resultPage);
            resultMap.put("totalCoursesCount", totalCoursesCount);
            resultMap.put("averageGrade", averageGrade);

            return R.success("查询成功", resultMap);
        }
    }

    @Override
    public R getCoursesNameById(String studentId, IPage<Map<String, Object>> page) {
//        // 创建查询条件，指定 studentId
//        LambdaQueryWrapper<StudentCourse> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(StudentCourse::getStudentId, studentId);

        // 执行分页查询
        IPage<Map<String, Object>> resultPage = studentCourseMapper.findCoursesByStudentId(page, studentId);

        // 检查结果是否为空，并构建响应
        if (resultPage.getRecords().isEmpty()) {
            return R.error("未找到该学生的课程成绩");
        } else {
            return R.success("查询成功", resultPage);
        }
    }
}
